package com.bbk.lling.recyclerview;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class GridLayoutActivity extends ActionBarActivity {
    private RecyclerView mRecyclerView;
    private ListAdapter mListAdapter;
    private List<MyItem> mDatas;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_grid_layout);
        // 初始化数据
        initData();
        // RecyclerView 控件
        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview);
        // ListAdapter
        mListAdapter = new ListAdapter(this, mDatas);
        // 监听
        mListAdapter.setOnItemClickListener(new ListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                //Toast.makeText(GridLayoutActivity.this, "Click" + mDatas.get(position), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onItemLongClick(View view, int position) {
                //mListAdapter.remove(position); //remove the item
                //Toast.makeText(GridLayoutActivity.this, "LongClick" + mDatas.get(position), Toast.LENGTH_SHORT).show();
            }
        });
        mRecyclerView.setAdapter(mListAdapter);
        mRecyclerView.setLayoutManager(new GridLayoutManager(this, 3));
        mRecyclerView.addItemDecoration(new GridItemDecoration(this));
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
    }

    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent event) {
        /* Do something */
        //mListAdapter = new ListAdapter(this, mDatas);
        // 监听

        mRecyclerView.setAdapter(mListAdapter);

    };


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_grid_layout, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.add_first:
                //mListAdapter.add(0, "add first");
                break;
            case R.id.add_last:
                //mListAdapter.add(mListAdapter.getItemCount(), "add last");
                break;
            case R.id.remove_first:
                //String value = mListAdapter.remove(0);
                //Toast.makeText(GridLayoutActivity.this, "remove:" + value, Toast.LENGTH_SHORT).show();
                break;
            case R.id.remove_last:
                //String value1 =  mListAdapter.remove(mListAdapter.getItemCount()-1);
                //Toast.makeText(GridLayoutActivity.this, "remove:" + value1, Toast.LENGTH_SHORT).show();
                break;
            case R.id.horizontal:
                mRecyclerView.setLayoutManager(new GridLayoutManager(this, 3, LinearLayoutManager.HORIZONTAL, false));
                break;
            case R.id.vertical:
                mRecyclerView.setLayoutManager(new GridLayoutManager(this, 3));
                break;
        }
        return super.onOptionsItemSelected(item);
    }

     /* ==========This Part is not necessary========= */

    /**
     * Create datas
     */
    protected void initData() {
        mDatas = new ArrayList<MyItem>();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 1; i < 4; i++) {
                    //到服务器上请求图片
                    OkHttpClient client = new OkHttpClient();
                    //3, 发起新的请求, 得到响应对象
                    Request request = new Request.Builder()
                            .url("http://p.hankunit.com/api/v1/get_picture/?picture_id=" + String.valueOf(i))
                            .build();
                    Response response = null;
                    try {
                        response = client.newCall(request).execute();
                        if (response.isSuccessful()) {
                            //这里是把图片存储在一个内存里
                            byte[] arr = response.body().bytes();
                            MyItem item = new MyItem();
                            //转化为bitmap
                            Bitmap bitmap = BitmapFactory.decodeByteArray(arr, 0, arr.length);
                            item.setPicture(bitmap);
                            item.setCheck(false);
                            mDatas.add(item);
                            MessageEvent evnet = new MessageEvent();
                            evnet.setEvent("Update");
                            EventBus.getDefault().post(evnet);
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

    /* ==========This Part is not necessary========= */
}
